<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增公众号信息' : '编辑公众号信息'"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="110px" style="margin: 0 50px;">
      <el-form-item label="公众号appId:" prop="accountAppId">
        <el-input v-model="temp.accountAppId" />
      </el-form-item>
      <el-form-item label="公众号appS:" prop="accountAppSecret">
        <el-input v-model="temp.accountAppSecret" />
      </el-form-item>
      <el-form-item label="公众号描述:" prop="accountDesc">
        <el-input v-model="temp.accountDesc" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { wechatAdd } from '@/api'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        type: 'add',
        temp: {
          accountAppId: '',
          accountAppSecret: '',
          accountDesc: ''
        },
        rules: {
          accountAppId: [{ required: true, message: '请输入公众号AppId', trigger: 'change' }],
          accountAppSecret: [{ required: true, message: '请输入公众号AppSecret', trigger: 'change' }],
          accountDesc: [{ required: true, message: '请输入公众号描述', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.type = 'edit'
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            accountAppId: this.temp.accountAppId,
            accountAppSecret: this.temp.accountAppSecret,
            accountDesc: this.temp.accountDesc
          }
          if (this.temp.id && this.temp.id > 0) params.id = this.temp.id
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await wechatAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await wechatAdd(params)
        this.resolve(this.temp)
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          accountAppId: '',
          accountAppSecret: '',
          accountDesc: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
